"""author: wenyao
   data: 2021/4/9
   project: devopscmdb
"""
#引用计数
#当有新的引用指向的时候，引用计数+1
#当有无效的引用发生的时候，引用计数-1
#当对象的引用计数为0的时候，销毁对象
#
# >>> from sys import getrefcount
# >>> a = 500
# >>> print(getrefcount(a))
# 2
# >>> b = a
# >>> print(getrefcount(a))
# 3
# >>>
# >>> lst = [ a]
# >>> print(getrefcount(a))
# 3
# >>> c = 600
# >>> d = 600
# >>> print(getrefcount(c))
# 2
# >>> print(getrefcount(c))
# 2

# >>> lst = [1,2,3,4]
# >>> del lst[1:3]
# >>> lst
# [1, 4]
# >>> c
# 600
# >>> del c
# >>> c
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# NameError: name 'c' is not defined

#######循环引用
# >>> x = [1]
# >>> y = [2]
# >>> x.append(y)
# >>> print(getrefcount(y))
# 3
# >>> print(getrefcount(x))
# 2
# >>> y.append(x)
# >>> print(getrefcount(x))
# 3
# >>> del x
# >>> del y
# >>> print(x)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# NameError: name 'x' is not defined

#内存泄漏：（占着茅坑不拉屎） 有一部内存无法被回收释放，进程又无法访问
#内存溢出（out of memory）：  内存不够用，程序需要的内存大于系统空闲内存

#########垃圾回收
#分代回收
#标记清除

########内存池
#小整型数据[-5,256]
# >>> a = 3
# >>> b = 3
# >>> id(a)
# 139821039101216
# >>> id(b)
# 139821039101216
# >>> a=300
# >>> b=300
# >>> id(a)
# 139821014258672
# >>> id(b)
# 139821014258480


#字符串驻留区
#多个字符，含有特殊字符的字符串不会放在驻留区
# >>> str1="abc"
# >>> str2 = "abc"
# >>> id(str1)
# 139821045187672
# >>> id(str2)
# 139821045187672
# >>> str1 = "abc abc"
# >>> str2 = "abc abc"
# >>> id(str1)
# 139821014481080
# >>> id(str2)
# 139821014481024



#深浅拷贝
#浅拷贝，只会拷贝第一层地址
# >>> b = a.copy()
# >>> b
# {'a': [1, 2], 'b': 2}
# >>> a
# {'a': [1, 2], 'b': 2}
# >>> b["b"]=4
# >>> b
# {'a': [1, 2], 'b': 4}
# >>> a
# {'a': [1, 2], 'b': 2}
# >>> b["a"].append(3)
# >>> b
# {'a': [1, 2, 3], 'b': 4}
# >>> a
# {'a': [1, 2, 3], 'b': 2}
# >>> lst = [[]]*3
# >>> lst
# [[], [], []]
# >>> lst[0]
# []
# >>> lst[0].append(1)
# >>> lst
# [[1], [1], [1]]
# >>> id(b["a"])
# 139821045550728
# >>> id(a["a"])
# 139821045550728
# >>> id(lst[0])
# 139821014478728
# >>> id(lst[1])
# 139821014478728
# >>> id(lst[2])
# 139821014478728


#深拷贝
# >>> import copy
# >>> a
# {'a': [1, 2, 3], 'b': 2}
# >>> b = copy.deepcopy(a)
# >>> a
# {'a': [1, 2, 3], 'b': 2}
# >>> b
# {'a': [1, 2, 3], 'b': 2}
# >>> b["a"].append(4)
# >>> b
# {'a': [1, 2, 3, 4], 'b': 2}
# >>> a
# {'a': [1, 2, 3], 'b': 2}
